package com.huashi.dealer.modular.controller.api.category;

import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.category.param.CategoryListVo;
import com.huashi.dealer.modular.business.category.service.CategoryService;
import com.huashi.dealer.modular.business.category.param.CategoryResponseParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * API分类控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API分类控制器")
@RestController("apiCategoryController")
@RequestMapping("/api/category")
@Validated
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 分类页面 - 对应PHP的 /category/index
     */
    @GetMapping("/index")
    @Operation(summary = "分类页面")
    public CommonResult<CategoryListVo> getCategoryIndex() {
        try {
            CategoryListVo responseParam = categoryService.getCategoryIndex();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取分类页面失败：" + e.getMessage());
        }
    }

    /**
     * 商品分类列表
     */
    @GetMapping("/list")
    @Operation(summary = "商品分类列表")
    public CommonResult<CategoryResponseParam> getCategoryList(@RequestParam(required = false) String token,
                                                              @RequestParam(required = false) Long parentId) {
        try {
            CategoryResponseParam responseParam = categoryService.getCategoryList(token, parentId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取商品分类列表失败：" + e.getMessage());
        }
    }

    /**
     * 分类详情
     */
    @GetMapping("/detail/{categoryId}")
    @Operation(summary = "分类详情")
    public CommonResult<CategoryResponseParam> getCategoryDetail(@PathVariable Long categoryId,
                                                                @RequestParam(required = false) String token) {
        try {
            CategoryResponseParam responseParam = categoryService.getCategoryDetail(categoryId, token);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取分类详情失败：" + e.getMessage());
        }
    }

    /**
     * 分类树形结构
     */
    @GetMapping("/tree")
    @Operation(summary = "分类树形结构")
    public CommonResult<CategoryResponseParam> getCategoryTree() {
        try {
            CategoryResponseParam responseParam = categoryService.getCategoryTree();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取分类树形结构失败：" + e.getMessage());
        }
    }
} 